//
//  HSLotteryAwardInfoVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/29.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSLotteryAwardInfoVC.h"
#import "HSLotteryAwardCell.h"
#import "HSWebVC.h"

@interface HSLotteryAwardInfoVC ()<UITableViewDelegate, UITableViewDataSource>

//@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<HSLotteryAwardModel *> *models;
//@property (nonatomic, assign) NSUInteger page;

@end

@implementation HSLotteryAwardInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.models = @[].mutableCopy;
    self.hintView.reloadBlock();
}

- (void)refresh
{
    NSDictionary *params = @{@"winner_list": @{
        @"page_index": @1,
        @"page_size": @20
    },@"c_head": @{
           @"client_id": @"BY003000000000000002",
            @"client_os": @"IOS"
        }};
    @weakify(self);
    [[MYHttpHelper shareHelper] postWithUrl:@"http://mycp.iplay78.com/trade-web/web/lottery/winners/list" params:params success:^(NSDictionary *dict) {
        NSDictionary *winner = dict[@"winner_list"];
        self.models = [NSArray modelArrayWithClass:[HSLotteryAwardModel class] json:winner[@"data"]].mutableCopy;
        @strongify(self);
//        self.page = 1;
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

- (void)loadmore
{
    NSDictionary *params = @{@"winner_list": @{
                                     @"page_index": @0,
                                     @"page_size": @20
                                     },@"c_head": @{
                                     @"client_id": @"BY003000000000000002",
                                     @"client_os": @"IOS"
                                     }};
    @weakify(self);
    [[MYHttpHelper shareHelper] postWithUrl:@"http://mycp.iplay78.com/trade-web/web/lottery/winners/list" params:params success:^(NSDictionary *dict) {
        @strongify(self);
        NSDictionary *winner = dict[@"winner_list"];
        [self.models addObjectsFromArray:[NSArray modelArrayWithClass:[HSLotteryAwardModel class] json:winner[@"data"]]];
//        self.page += 1;
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSLotteryAwardCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HSLotteryAwardCell class])];
    cell.model = self.models[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self my_push:[[HSWebVC alloc] initWithUrl:self.models[indexPath.row].w_info_absolute_url]];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - NavBarHeight - TabbarHeight - kYSLMenuHeight)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView addRefreshTarget:self action:@selector(refresh)];
        [_tableView addLoadMoreTarget:self action:@selector(loadmore)];
        [_tableView registerClass:[HSLotteryAwardCell class] forCellReuseIdentifier:NSStringFromClass([HSLotteryAwardCell class])];
    }
    return _tableView;
}

@end
